package com.mashibing.juc.c_019;

import java.util.concurrent.TimeUnit;

/**
 * ReentrantLock可重入锁
 * ReentrantLock用于替代synchronized
 * 可重入：锁了一次之后，还能对同样一把锁再锁第二次。
 * @author: Yuanzt
 * @Date: 2021/3/26
 */
public class T01_ReentrantLock1 {

    synchronized void m1(){
        for (int i = 0; i < 10; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i+" thread: "+Thread.currentThread().getName());
            if (i == 5) m2();
        }
    }

    synchronized void m2(){
        System.out.println(Thread.currentThread().getName()+" m2...");
    }

    public static void main(String[] args) {
        T01_ReentrantLock1 tr = new T01_ReentrantLock1();
        new Thread(tr::m1,"t1").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        new Thread(tr::m2,"t2").start();
    }
}
